package pe.com.cineplanet.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;

import pe.com.cineplanet.bean.Company;
import pe.com.cineplanet.bean.Politic;
import pe.com.cineplanet.bean.Role;
import pe.com.cineplanet.bean.User;
import pe.com.cineplanet.bean.cineplanet.Constantes;
import pe.com.cineplanet.bean.cineplanet.Pelicula;
import pe.com.cineplanet.cons.Const;
import pe.com.cineplanet.dao.dao.PeliculaDAO;
import pe.com.cineplanet.form.LoginForm;
import pe.com.cineplanet.service.Service;
import pe.com.rix.taglib.bean.menu.MenuItem;

public class LoginController extends MultiActionController {

	private Service service;
	private LoginForm form;
	private PeliculaDAO peliculaDAO;
	
	public void setPeliculaDAO(PeliculaDAO peliculaDAO) {
		this.peliculaDAO = peliculaDAO;
	}
	private Class<?> commandClass;  
	
	public LoginController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("view");
        setMethodNameResolver(resolver);
    }
	
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("login/view");
    } 
	
	
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		
		form = getForm(request);
		
		String localeLang = "es_PE";
		
		//User user = getService().login(form.getUser(), form.getPwd());
		User user = null;
		if( !(form.getUser().trim().length()==0 || form.getPwd().trim().length()==0) ){

			user = new User(1, "Ricardo Martin","Rodriguez Garcia", new Role(1, "Administrador", new Company(1, "Cliente VIP", "es_PE")),"rix_xtr@hotmail.com", true);
			user.setLogin( form.getUser() );
			List<Politic> list = Constantes.defaultMenu();
			list.add(new Politic(5, "Seguridad", 1, 0, 0));
			list.add(new Politic(6, "Usuarios", 0, 5, 4));
			list.add(new Politic(7, "Perfiles", 1, 5, 5));
			list.add(new Politic(8, "Permisos", 1, 5, 6));			
			user.getRole().setPolitics(list);
		}
		
		if(user==null){
			ModelAndView model = view(request, response);
			model.addObject("msg", "nologin");
			return model;
		}else if(user.isActive()==false){
			ModelAndView model = view(request, response);
			model.addObject("msg", "noactive");
			return model;
		}
		
		localeLang = user.getRole().getCompany().getLocale();
		String[] localesrc = localeLang.split("_");
			
		Locale locale = new java.util.Locale(localesrc[0], localesrc[1]); 
		HttpSession session = request.getSession(); 
		session.setAttribute(Const.SESSION_KEY, user);
		session.setAttribute(Globals.LOCALE_KEY, locale);
		ModelAndView model = new ModelAndView("index/loading");
		model.addObject("redirect","login.htm?method=load");
		return model;

    } 
	
	
	public ModelAndView load(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		HttpSession session = request.getSession(); 
		User user = (User)session.getAttribute(Const.SESSION_KEY);
		ModelAndView model = index(request, response);
		
		if(user!=null){
			MenuItem root = new MenuItem();
			root.setValues(Constantes.formatMenu(user.getRole().getPolitics()));
			session.setAttribute("menuRoot", root);
		}
		
		List<Pelicula> peliculas = peliculaDAO.findLast();
		
		model.addObject("listPelicula", peliculas);
		model.addObject("pages", 1);
		model.addObject("lastpage", 1);
		
		
		return model;
		
	}
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		return new ModelAndView("index/view");
	}
	
	@SuppressWarnings("unchecked")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    	HttpSession session = request.getSession();
    	Enumeration<String> attrs = session.getAttributeNames();
    	while(attrs.hasMoreElements()){
    		String attrName = attrs.nextElement();
    		session.removeAttribute(attrName);
    	}
    	session.invalidate();
    	return load(request, response);
		
	}
	
	public ModelAndView config(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		ModelAndView model =  new ModelAndView("login/config");
		return model;
		
	}
	
	public ModelAndView configData(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		ModelAndView model =  new ModelAndView("login/config/info");
		
		String tipo = request.getParameter("tipo");
		System.out.println("tipo: " + tipo);
		if(tipo==null || tipo.length()==0) tipo = "info";
		if(tipo.equals("editar")){
			List<String> rubros = new ArrayList<String>();
			rubros.add("Rubro 1");
			rubros.add("Rubro 2");
			model.addObject("listRubros", rubros);
		}
		
		model.addObject("tipo", tipo);
		
		return model;
		
	}
	
	
	public ModelAndView expired(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		
		ModelAndView model =  view(request, response);
		model.addObject("msg", "expired");
		return model;
		
	}
	
	
	public Object getCommandObject(HttpServletRequest request)
	throws Exception {
		Object command = formBackingObject(request);
		bind (request, command);
		return command;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		return commandClass.newInstance();
	}

	public Class<?> getCommandClass() {
		return commandClass;
	}

	public void setCommandClass(Class<?> commandClass) {
		this.commandClass = commandClass;
	}
	
	public void setService(Service service) {
		this.service = service;
	}
	
	public Service getService() {
		return service;
	}
	
	public LoginForm getForm(HttpServletRequest request) throws Exception {
		Object command = getCommandObject(request);
    	form = (LoginForm)command;
		return form;
	}
	
}
